<?php
require_once "HTTP/Upload.php";
require_once 'Image/Transform.php';

$objCarForm = new BM_PHPForm();
$objCarForm->UseLocator();
$objCarForm->SetIndent("		");
$entCar = CarDao::find($id);
$entCar = CarDao::findCarOptions($entCar);
$arrOptions = CarDao::findAllOptions();
?>
<div id="sub">
  <a href="../<?php echo $www;?>/index_public.php?entity=car&action=details&cid=<?php echo $entCar->id;?>">
  <img alt="<?php print $entCar->photo?>" src="img/cars/<?php print $entCar->photo?>" width="300px" height="200px"/></a>
</div>
<?php

if ($objCarForm->IsPost()) {
	
	if ($objCarForm->IsValid()) {
		$upload = new HTTP_Upload("en");
		$upload->setChmod(0644);
		$it = Image_Transform::factory('GD');
		$filePhoto = $upload->getFiles('photo');
		$filePic1 = $upload->getFiles('pic1');
		$filePic2 = $upload->getFiles('pic2');
		$filePic3 = $upload->getFiles('pic3');
		$filePic4 = $upload->getFiles('pic4');
		if ($filePhoto->isValid()) {
			$filePhoto->setName("uniq");
	        $dest_photo = $filePhoto->moveTo("img/cars/");
	        $photo = $filePhoto->getProp('name');
	        $it->load("img/cars/$photo");
	        $it->scaleMaxLength(100);
	        $it->save("img/cars/_thb_".$photo);
			$tmpPhoto = "img/cars/$entCar->photo";
			$thbPhoto = "img/cars/_thb_$entCar->photo";
			if (file_exists($tmpPhoto)&& is_file($tmpPhoto)) { unlink ($tmpPhoto); }
			if (file_exists($thbPhoto)&& is_file($thbPhoto)) { unlink ($thbPhoto); }
		}else {
			$photo = $entCar->photo;
		}
		if ($filePic1->isValid()) {
			$filePic1->setName("uniq");
			$dest_pic1 = $filePic1->moveTo("img/cars/");
			$pic1 = $filePic1->getProp('name');
			$it->load("img/cars/$pic1");
	        $it->scaleMaxLength(100);
	        $it->save("img/cars/_thb_".$pic1);
			$tmpPic1 = "img/cars/$entCar->pic1";
			$thbPic1 = "img/cars/_thb_$entCar->pic1";
			if (file_exists($tmpPic1)&& is_file($tmpPic1)) { unlink ($tmpPic1); }
			if (file_exists($thbPic1)&& is_file($thbPic1)) { unlink ($thbPic1); }
		}else {
			$pic1 = $entCar->pic1;
		}
		if ($filePic2->isValid()) {
			$filePic2->setName("uniq");
			$dest_pic2 = $filePic2->moveTo("img/cars/");
			$pic2 = $filePic2->getProp('name');
			$it->load("img/cars/$pic2");
	        $it->scaleMaxLength(100);
	        $it->save("img/cars/_thb_".$pic2);
			$tmpPic2 = "img/cars/$entCar->pic2";
			$thbPic2 = "img/cars/_thb_$entCar->pic2";
			if (file_exists($tmpPic2)&& is_file($tmpPic2)) { unlink ($tmpPic2); }
			if (file_exists($thbPic2)&& is_file($thbPic2)) { unlink ($thbPic2); }
		}else {
			$pic2 = $entCar->pic2;
		}
		if ($filePic3->isValid()) {
			$filePic3->setName("uniq");
			$dest_pic3 = $filePic3->moveTo("img/cars/");
			$pic3 = $filePic3->getProp('name');
			$it->load("img/cars/$pic3");
	        $it->scaleMaxLength(100);
	        $it->save("img/cars/_thb_".$pic3);
			$tmpPic3 = "img/cars/$entCar->pic3";
			$thbPic3 = "img/cars/_thb_$entCar->pic3";
			if (file_exists($tmpPic3)&& is_file($tmpPic3)) { unlink ($tmpPic3); }
			if (file_exists($thbPic3)&& is_file($thbPic3)) { unlink ($thbPic3); }
		}else {
			$pic3 = $entCar->pic3;
		}
		if ($filePic4->isValid()) {
			$filePic4->setName("uniq");
			$dest_pic4 = $filePic4->moveTo("img/cars/");
			$pic4 = $filePic4->getProp('name');
			$it->load("img/cars/$pic4");
	        $it->scaleMaxLength(100);
	        $it->save("img/cars/_thb_".$pic4);
        	$tmpPic4 = "img/cars/$entCar->pic4";
        	$thbPic4 = "img/cars/_thb_$entCar->pic4";
			if (file_exists($tmpPic4)&& is_file($tmpPic4)) { unlink ($tmpPic4); }
			if (file_exists($thbPic4)&& is_file($thbPic4)) { unlink ($thbPic4); }
		}else {
			$pic4 = $entCar->pic4;
		}
		$entCar = new Car($id,$objCarForm->arrPostData['make'],$objCarForm->arrPostData['model'], $objCarForm->arrPostData['price'],$objCarForm->arrPostData['color'],$photo);
		$entCar->setPics($pic1,$pic2,$pic3,$pic4);
		$entCar->setDetails($objCarForm->arrPostData['vehicle'],$objCarForm->arrPostData['door'],
							$objCarForm->arrPostData['cylind'],$objCarForm->arrPostData['fuel'],
							$objCarForm->arrPostData['gear'],$objCarForm->arrPostData['gearbox'],
							$objCarForm->arrPostData['odometer'],$objCarForm->arrPostData['rego'],
							$objCarForm->arrPostData['stock'],$objCarForm->arrPostData['utime'],
							$objCarForm->arrPostData['vin'],$objCarForm->arrPostData['comments']);
		if (CarDao::update($entCar)) {
			$options = array();
			foreach($arrOptions as $option){
				$optionId = $option['id'];
				if(isset($objCarForm->arrPostData[$optionId])){
					$options[]=$optionId;
				}
			}
			if (!sizeof($options) == 0){
				CarDao::saveOptions($entCar->id,$options);
			}
			$objCarForm->SetInfor("submit", "Car Updated Successfully !");
		}else {
			$objCarForm->SetError("submit","Error Accour!! Car did not updated.");
		}
	}
}

print $objCarForm->ErrMsgs();
print $objCarForm->InfMsgs();

print $objCarForm->Open("carform","Car Update Form");
print $objCarForm->HiddenField("action" ,"$action");
print $objCarForm->HiddenField("entity","$entity");
print $objCarForm->HiddenField("id", $id);

print $objCarForm->TextBox("make"  ,"Make:" ,"$entCar->make");
print $objCarForm->TextBox("model" ,"Model:","$entCar->model");
print $objCarForm->TextBox("price" ,"Price:","$entCar->price");
print $objCarForm->TextBox("color" ,"Color:","$entCar->color");
print $objCarForm->File("photo","Main Picture:");
print $objCarForm->File("pic1","Picture1:");
print $objCarForm->File("pic2","Picture2:");
print $objCarForm->File("pic3","Picture3:");
print $objCarForm->File("pic4","Picture4:");
print $objCarForm->TextBox("vehicle" ,"Vehicle:","$entCar->vehicle");
print $objCarForm->TextBox("door" ,"Doors:","$entCar->door");
print $objCarForm->TextBox("cylind" ,"Cylinders:","$entCar->cylind");
print $objCarForm->TextBox("fuel" ,"Fuel Type:","$entCar->fuel");
print $objCarForm->TextBox("gear" ,"Gears:","$entCar->gear");
print $objCarForm->TextBox("gearbox" ,"Gearbox:","$entCar->gearbox");
print $objCarForm->TextBox("odometer" ,"Odometer:","$entCar->odometer");
print $objCarForm->TextBox("rego" ,"Rego:","$entCar->rego");
print $objCarForm->TextBox("stock" ,"Stock:","$entCar->stock");
//print $objCarForm->TextBox("utime" ,"Last-Updated:","");
print $objCarForm->TextBox("vin" ,"VIN:","$entCar->vin");
print $objCarForm->TextArea("comments","Comments:","$entCar->comments");
print "&nbsp;&nbsp;&nbsp;&nbsp;Vehicle Options:";
print "<hr/>";
foreach ($arrOptions as $option) {
	if (in_array($option['id'], $entCar->getOptions())) {
		$arrCheckbox[] = $objCarForm->CheckBox2($option['id'],$option['name'],array(),true,$option['id']);
	}else {
		$arrCheckbox[] = $objCarForm->CheckBox2($option['id'],$option['name'],array(),false,$option['id']);
	}
}
?>
<table>
<?php 
for ($i=0; $i<count($arrCheckbox); $i+=3) {
?>
<tr>
    <td><?php echo $arrCheckbox[$i]?></td>
    <td><?php echo $arrCheckbox[$i+1]?></td>
    <td><?php echo $arrCheckbox[$i+2]?></td>
</tr>
<?php	
}
?>
</table>
<?php
print $objCarForm->SubmitButton("submit", "Update", array(), "submit");
print $objCarForm->Close();
?>